﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPMonitor.Interfaces;
using NPMonitor.DataModel;
using NPMonitor.Implementation.Internal;

namespace NPMonitor.Implementation.Store
{
    public class MailStore : IMailStore
    {
        /// <summary>
        /// 添加邮件
        /// </summary>
        /// <param name="mailData"></param>
        public void AddMail(MailData mailData)
        {
            using (var ctx = new DbStoreContext())
            {
                ctx.Mails.Add(mailData);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// 获取未发送的邮件
        /// </summary>
        /// <param name="takeCount"></param>
        /// <returns></returns>
        public List<MailData> GetNoSendMailList(int takeCount)
        {
            using (var ctx = new DbStoreContext())
            {
                int status = (int)MailStatus.NoSend;
                return ctx.Mails.Where(p => p.SendStatus == status).Take(takeCount).ToList();
            }
        }

        /// <summary>
        /// 更改Mail状态
        /// </summary>
        /// <param name="mailId"></param>
        /// <param name="status"></param>
        public void UpdateMail(int mailId, MailStatus status)
        {
            using (var ctx = new DbStoreContext())
            {
                MailData data = ctx.Mails.FirstOrDefault(p => p.MailId == mailId);
                if (data != null)
                {
                    data.SendStatus = (int)status;
                    ctx.SaveChanges();
                }
            }
        }
    }
}
